package com.example.checkqufuface.Customer;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.checkqufuface.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.Adapter.BindKaAdapter;
import com.example.checkqufuface.R;
import com.example.checkqufuface.bean.QufufTerminalTypeListResultBean;

import java.util.List;

public class SettingBindDialog extends BottomDialogBase implements View.OnClickListener {

    private Context context;

    private ComfirmListener comfirmListener;
    private QuxiaoListener quxiaoListener;
    private final BindKaAdapter sexAdapter;
    private TextView teBind;
    private RecyclerView rlBind;
    private TextView confim;
    private TextView quxiao;
    private int selectNumber = -1;
    private List<QufufTerminalTypeListResultBean.DataBean> terminalList;
    private ImageView close;


    public SettingBindDialog(Context context, List<QufufTerminalTypeListResultBean.DataBean> terminalList) {
        super(context);
        this.context = context;
        this.terminalList = terminalList;

        String meationName = (String) SPUtils.get(context, Customer.QUFU_TERMINALNAME, "");
        teBind.setText(meationName);
        sexAdapter = new BindKaAdapter(context, terminalList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        rlBind.setLayoutManager(linearLayoutManager);
        rlBind.setAdapter(sexAdapter);

        final List<QufufTerminalTypeListResultBean.DataBean> finalTerminalList = terminalList;
        sexAdapter.SetSexClickListener(new BindKaAdapter.SexClickListener() {
            @Override
            public void sexSelect(int pos) {
                teBind.setText(finalTerminalList.get(pos).getTerminalShortName());
                // 要保存绑定的终端名称，方便下次显示
                selectNumber = pos;
            }
        });


        confim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确定就把绑定终端号，发送出去
                // 如果没有选择东西的话，那么就提示用户，没有选择
                if (selectNumber != -1) {
                    comfirmListener.comfirm(finalTerminalList.get(selectNumber));
                } else {
                    SelfToastUtil.getInstance().show("请选择需要绑定的终端");
                }
            }
        });

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isNullOrEmpty(teBind.getText().toString())) {
                    //如果是空，就没有必要去取消
                    SelfToastUtil.getInstance().show("暂无绑定终端，无需取消操作");
                }else {
                    quxiaoListener.Quxiao();
                }
            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    protected void onCreate() {
        setContentView(R.layout.widget_make_bind);

        // 查找控件
        teBind = findViewById(R.id.tv_widget_bind);
        rlBind = findViewById(R.id.rl_tv_bind_selete);
        confim = findViewById(R.id.popup_queren_infor);
        quxiao = findViewById(R.id.popup_quxiao_infor);

        close = findViewById(R.id.iv_wg_qu_xiao_ka);


    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public interface ComfirmListener {
        void comfirm(QufufTerminalTypeListResultBean.DataBean number);
    }

    public void setComfirmListener(ComfirmListener comfirmListener) {
        this.comfirmListener = comfirmListener;
    }

    public interface QuxiaoListener {
        void Quxiao();
    }

    public void setQuxiaoListener(QuxiaoListener comfirmListener) {
        this.quxiaoListener = comfirmListener;
    }

}


